package servlet.ProductServlet;

import dao.ProductDAO;
import entity.PageBean;
import entity.ProductBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.io.IOException;
import java.util.List;

@WebServlet("/ProductListServlet")
public class ProductListServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String productCode = "";
        String productName = "";
        String batch = "";
        String worker = "";
        String processingDate = "";
        String workshop = "";
        String productSize = "";
        String processingMethod = "";
        if (request.getParameter("productCode") != null) productCode = request.getParameter("productCode");
        if (request.getParameter("productName") != null) productName = request.getParameter("productName");
        if (request.getParameter("batch") != null) batch = request.getParameter("batch");
        if (request.getParameter("worker") != null) worker = request.getParameter("worker");
        if (request.getParameter("processingDate") != null) processingDate = request.getParameter("processingDate");
        if (request.getParameter("workshop") != null) workshop = request.getParameter("workshop");
        if (request.getParameter("productSize") != null) productSize = request.getParameter("productSize");
        if (request.getParameter("processingMethod") != null) processingMethod = request.getParameter("processingMethod");

        ProductDAO dao = new ProductDAO(productCode, productName, batch, worker, processingDate, workshop, productSize, processingMethod);
        int pageCount = dao.findPageCount();
        int alterpage = 0;
        if (request.getParameter("alterpage") != null) {
            alterpage = Integer.parseInt(request.getParameter("alterpage"));
        }
        int curpage = 1;
        if (request.getParameter("page") != null) {
            curpage = Integer.parseInt(request.getParameter("page"));
        }
        if (curpage > 1 || alterpage > 0) {
            curpage += alterpage;
        }

        if(curpage > pageCount){
            curpage = pageCount;
        }
        PageBean pageBean = new PageBean(curpage, alterpage, pageCount);
        List<ProductBean> productList = dao.findProduct(curpage);
        request.setAttribute("productCode", productCode);
        request.setAttribute("productName", productName);
        request.setAttribute("batch", batch);
        request.setAttribute("worker", worker);
        request.setAttribute("workshop", workshop);
        request.setAttribute("productList", productList);
        request.setAttribute("page", curpage);
        request.setAttribute("pageCount", pageCount);
        request.setAttribute("pageBean", pageBean);
        request.getRequestDispatcher("product_list.jsp").forward(request, response);
    }
}